#include "GameController.h"

#include <SFML/Graphics.hpp>

#include "BuildingSystem/BuildingManager.h"
#include "GridMap/GridMap.h"
#include "HUD/HUD.h"

GameController::GameController()
{
    //ctor
}

void GameController::run(sf::RenderWindow& window)
{
    sf::Clock clock;
    while (window.isOpen ()){
        sf::Event event;
        while (window.pollEvent (event)){
            switch (event.type){
                case sf::Event::MouseButtonPressed:
                    if (sf::Mouse::isButtonPressed (sf::Mouse::Left)) {
                        bool eventFound = false;
                        eventFound = BuildingManager::getInstance ().processLeftClickEvent (sf::Mouse::getPosition (window));
                        /*if(eventFound == false)
							UnitManager::getInstance ().processMouseEvent(sf::Mouse::getPosition(window));*/
                        /*if(eventFound == false)
							GridMap::getInstance ().processMouseEvent(sf::Mouse::getPosition(window));*/
                        /*if(eventFound == false)
							HUD::getInstance ().processMouseEvent(sf::Mouse::getPosition(window));*/
                    }
                    break;
				case sf::Event::Closed:
                    window.close();
                    break;

                default:
                    break;
            }
        }
        if (clock.getElapsedTime () > sf::milliseconds (16.66667)) //maximum 60 update/sec
            updateGame (clock.restart ());

        window.clear ();
        drawAll (window);
        window.display ();
   }
}

void GameController::updateGame(sf::Time elapsed)
{
}

void GameController::drawAll(sf::RenderWindow& window) const
{
	window.draw (GridMap::getInstance ());
	window.draw (HUD::getInstance ());
	window.draw (BuildingManager::getInstance ());
	//window.draw (UnitManager::getInstance ());
}
